//
//  OYAlertView.m
//  qpyd
//
//  Created by 掌游网络 on 2017/7/20.
//  Copyright © 2017年 掌游网络. All rights reserved.
//

#import "OYAlertView.h"

///alertView  宽
#define AlertW 288
//button 高
#define kBtnH 56

///各个栏目之间的距离
#define XLSpace 10.0

@interface OYAlertView ()

//弹窗
@property (nonatomic, strong) UIView *alertView;
//title
@property (nonatomic, strong) UILabel *titleLbl;
//内容
@property (nonatomic, strong) UILabel *msgLbl;
//右边按钮
@property (nonatomic, strong) UIButton *rightBtn;
//左边按钮
@property (nonatomic, strong) UIButton *leftBtn;
//横线线
@property (nonatomic, strong) UIView *lineView;
//竖线
@property (nonatomic, strong) UIView *verLineView;

//回调
@property (nonatomic, copy) void(^actionBlock)(OYAlertViewActionType actionType);

@end

@implementation OYAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message leftBtn:(NSString *)leftTitle rightBtn:(NSString *)rightTitle actionBlock:(void(^)(OYAlertViewActionType actionType))actionBlock {

    if (self == [super init]) {
        self.actionBlock = actionBlock;
        self.frame = [UIScreen mainScreen].bounds;
        self.alpha = 0;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 2.0;
        
        self.alertView.frame = CGRectMake(0, 0, AlertW, 100);
        self.alertView.layer.position = self.center;
        
        if (title) {
            
            self.titleLbl = [self GetAdaptiveLable:CGRectMake(0, 0, AlertW-6*XLSpace, 20) AndText:title andIsTitle:YES];
            self.titleLbl.textColor = OYColorRGB(33, 40, 50);
            
            [self.alertView addSubview:self.titleLbl];
            
            CGFloat titleW = self.titleLbl.bounds.size.width;
            CGFloat titleH = self.titleLbl.bounds.size.height;
            
            self.titleLbl.frame = CGRectMake((AlertW - titleW) / 2, 3 * XLSpace, titleW, titleH);
            
        }
        if (message) {
            
            self.msgLbl = [self GetAdaptiveLable:CGRectMake(0, 0, AlertW - 4 * XLSpace, 20) AndText:message andIsTitle:NO];
            self.msgLbl.textColor = OYColorRGB(113, 120, 130);
            
            [self.alertView addSubview:self.msgLbl];
            
            CGFloat msgW = self.msgLbl.bounds.size.width;
            CGFloat msgH = self.msgLbl.bounds.size.height;
            
            self.msgLbl.frame = self.titleLbl ? CGRectMake((AlertW-msgW)/2, CGRectGetMaxY(self.titleLbl.frame) + XLSpace * 1.5, msgW, msgH) : CGRectMake((AlertW-msgW) / 2, 2*XLSpace, msgW, msgH);
        }
        
        self.lineView = [[UIView alloc] init];
        self.lineView.frame = self.msgLbl?CGRectMake(0, CGRectGetMaxY(self.msgLbl.frame)+2.5*XLSpace, AlertW, 0.5):CGRectMake(0, CGRectGetMaxY(self.titleLbl.frame)+2*XLSpace, AlertW, 0.5);
        self.lineView.backgroundColor = OYColorRGB(212, 214, 216);
        [self.alertView addSubview:self.lineView];
        
        //两个按钮
        if (leftTitle && rightTitle) {
            
            self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.leftBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2, kBtnH);
            [self.leftBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [self.leftBtn setBackgroundImage:[self imageWithColor:OYRGBFrom16(0xf9fafb)] forState:UIControlStateHighlighted];
            [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
            self.leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
            [self.leftBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateNormal];
            [self.leftBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateHighlighted];
            self.leftBtn.tag = 1000;
            [self.leftBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(2.0, 2.0)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.leftBtn.bounds;
            maskLayer.path = maskPath.CGPath;
            self.leftBtn.layer.mask = maskLayer;
            
            [self.alertView addSubview:self.leftBtn];
        }
        
        if (leftTitle && rightTitle) {
            self.verLineView = [[UIView alloc] init];
            self.verLineView.frame = CGRectMake(CGRectGetMaxX(self.leftBtn.frame), CGRectGetMaxY(self.lineView.frame), 0.5, kBtnH);
            self.verLineView.backgroundColor = OYColorRGB(212, 214, 216);
            [self.alertView addSubview:self.verLineView];
        }
        
        if(leftTitle && rightTitle){
            
            self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.rightBtn.frame = CGRectMake(CGRectGetMaxX(self.verLineView.frame), CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2+1, kBtnH);
            [self.rightBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundImage:[self imageWithColor:OYRGBFrom16(0xf9fafb)] forState:UIControlStateHighlighted];
            [self.rightBtn setTitle:rightTitle forState:UIControlStateNormal];
            self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
            [self.rightBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateNormal];
            [self.rightBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateHighlighted];
            self.rightBtn.tag = 1001;
            [self.rightBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.rightBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(2.0, 2.0)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.rightBtn.bounds;
            maskLayer.path = maskPath.CGPath;
            self.rightBtn.layer.mask = maskLayer;
            
            [self.alertView addSubview:self.rightBtn];
            
        }
        
        //只有取消按钮
        if (leftTitle && !rightTitle) {
            
            self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.leftBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, kBtnH);
            [self.leftBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [self.leftBtn setBackgroundImage:[self imageWithColor:OYRGBFrom16(0xf9fafb)] forState:UIControlStateHighlighted];
            [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
            self.leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
            [self.leftBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateNormal];
            [self.leftBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateHighlighted];
            self.leftBtn.tag = 1000;
            [self.leftBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(2.0, 2.0)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.leftBtn.bounds;
            maskLayer.path = maskPath.CGPath;
            self.leftBtn.layer.mask = maskLayer;
            
            [self.alertView addSubview:self.leftBtn];
        }
        
        //只有确定按钮
        if (rightTitle && !leftTitle) {
            
            self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.rightBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, kBtnH);
            [self.rightBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundImage:[self imageWithColor:OYRGBFrom16(0xf9fafb)] forState:UIControlStateHighlighted];
            [self.rightBtn setTitle:rightTitle forState:UIControlStateNormal];
            self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
            [self.rightBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateNormal];
            [self.rightBtn setTitleColor:OYRGBFrom16(0x1b88ee) forState:UIControlStateHighlighted];
            self.rightBtn.tag = 1001;
            [self.rightBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.rightBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(2.0, 2.0)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.rightBtn.bounds;
            maskLayer.path = maskPath.CGPath;
            self.rightBtn.layer.mask = maskLayer;
            
            [self.alertView addSubview:self.rightBtn];
            
        }
        
        //计算高度
        CGFloat alertHeight = leftTitle ? CGRectGetMaxY(self.leftBtn.frame) : CGRectGetMaxY(self.rightBtn.frame);
        self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
        self.alertView.layer.position = self.center;
        
        [self addSubview:self.alertView];
    }
    
    return self;
}

#pragma mark - 弹出 -
- (void)showAlertView {

    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

- (void)creatShowAnimation {

    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(1.15, 1.15);
    OYWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        OYStrongSelf
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
    }];
}

#pragma mark - 回调 -设置只有2  -- > 确定才回调
- (void)buttonEvent:(UIButton *)sender {

    switch (sender.tag) {
        case 1000: {
            if (self.actionBlock) {
                self.actionBlock(OYAlertViewActionTypeLeft);
            }
            break;
        }
        case 1001: {
            if (self.actionBlock) {
                self.actionBlock(OYAlertViewActionTypeRight);
            }
            break;
        }
            
        default:
            break;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle {

    UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
    contentLbl.numberOfLines = 0;
    contentLbl.text = contentStr;
    if (isTitle) {
        contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
    } else {
        contentLbl.font = [UIFont systemFontOfSize:13.0];
    }
    
    NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
    [mParaStyle setLineSpacing:5.0];
    [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0, [contentStr length])];
    [contentLbl setAttributedText:mAttrStr];
    contentLbl.textAlignment = NSTextAlignmentCenter;
    [contentLbl sizeToFit];
    
    return contentLbl;
}

- (UIImage *)imageWithColor:(UIColor *)color {

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

#pragma mark - public method
- (void)setLeftButtonTitleColor:(UIColor *)color {
    [self.leftBtn setTitleColor:color forState:UIControlStateNormal];
    [self.leftBtn setTitleColor:color forState:UIControlStateHighlighted];
}

- (void)setRightButtonTitleColor:(UIColor *)color {
    [self.rightBtn setTitleColor:color forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:color forState:UIControlStateHighlighted];
}

- (void)dealloc {
    
    OYLogFun
}

@end
